package com.vipsoft.locker.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vipsoft.base.core.BaseController;
import com.vipsoft.base.core.ApiResult;
import com.vipsoft.base.entity.locker.CategoryInfo;
import com.vipsoft.base.entity.locker.LocationInfo;
import com.vipsoft.locker.service.ILocationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 物品存放位置
 */
@RestController
@RequestMapping("/location")
public class LocationController extends BaseController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ILocationService locationService;


    @PostMapping("/save")
    public ApiResult save(HttpServletRequest request, @RequestBody LocationInfo param) {
        if (param == null) {
            return new ApiResult(6001, "传入参数无效");
        }
        if (StrUtil.isEmpty(param.getMemberId())) {
            return new ApiResult(6001, "必须传入用户编号");
        }
        boolean result = locationService.save(param);
        return new ApiResult(result);
    }

    @PostMapping("/list")
    public ApiResult list(HttpServletRequest request, @RequestBody Map<String, Object> param) {
        if (param == null) {
            return new ApiResult(6001, "传入参数无效");
        }
        if(!param.containsKey("memberId__eq")){
            return new ApiResult(6001, "缺少 MemberId 参数");
        }
        Page page = buildPage(param, CategoryInfo.class);
        QueryWrapper<LocationInfo> queryWrapper = buildQueryWrapper(LocationInfo.class, param);
        queryWrapper.lambda().or().eq(LocationInfo::getMemberId, "-1");
        IPage pageList = locationService.page(page, queryWrapper);
        return new ApiResult(pageList);
    }
}
